/**
 * Created by yongwang on 2015/9/16.
 */
angular.module('mis.integralStrategyManager.controllers', [])
    .controller('integralStrategyCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {
        $scope.currentPage = 1;
        $scope.pageSize = 10;

        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/integralStrategy/integralStrategyList/:page/:size',
            ajax: $http,
            success:function(data){
                if(data && data.data) {
                    $scope.integralStrategyList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                //$scope.menuList = null;
            },
            params: {}
        });

        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };
        //删除
        $scope.delete = function(){
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/integralStrategy/integralStrategyDelete',
                checkObj: $(".item-check"),//checkbox对象
                key: "integralStrategyID",
                success: function(data){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);
                    alert("删除成功！");
                },
                error: function(){}
            };
            $scope.pagination.listDelete(params);
        };
        //新增
        $scope.addData = {};
        $scope.add = function(addData){
            if(!addData.item_type){alert("请输入积分来源项的类型！");return;}
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/integralStrategy/integralStrategyAdd',
                addData: addData,
                success: function(data){
                    $scope.pagination.selectPage(1);//插入成功显示第一页
                    $('#doc-modal-add').modal("close");
                    if(data.result.status == 1){
                        alert("新增成功！");
                        $scope.addData = {};
                    }else{
                        alert("新增失败！");
                    }
                },
                error: function(){
                    $('#doc-modal-add').modal("close");
                    alert("新增失败！");
                }
            };
            $scope.pagination.listAdd(params);
        };
        //编辑
        $scope.update = function(updateData,index){
            var uData = {integralStrategyID:"",integralStrategy:{}};
            if(updateData.id)uData.integralStrategyID = updateData.id;
            if(updateData.item_type != null)uData.integralStrategy.item_type = updateData.item_type;else {alert("请输入积分来源项的类型！");return;}
            if(updateData.item_name)uData.integralStrategy.item_name = updateData.item_name;
            if(updateData.item_value != null)uData.integralStrategy.item_value = updateData.item_value;
            if(updateData.item_value_limit_day)uData.integralStrategy.item_value_limit_day = updateData.item_value_limit_day;
            if(updateData.item_value_limit_month != null)uData.integralStrategy.item_value_limit_month = updateData.item_value_limit_month;
            if(updateData.name)uData.integralStrategy.name = updateData.name;
            if(updateData.is_single_limit)uData.integralStrategy.is_single_limit = updateData.is_single_limit;
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/mis/integralStrategy/integralStrategyEdit',
                updateData: uData,
                success: function(data){
                    var pageNumber = $(".am-active").text();//当前页码
                    $('#doc-modal-'+index).modal("close");
                    $scope.pagination.selectPage(pageNumber);
                    if(data.result.status == 1){
                        alert("修改成功！");
                    }else{
                        alert("修改失败！");
                    }
                },
                error: function(){
                    var number = $(".am-active").text();//当前页码
                    $('#doc-modal-'+index).modal("close");
                    alert("修改失败！");
                }
            };
            $scope.pagination.listUpdate(params);
        };

        //搜索
        $scope.search = function () {

            var opt ={
                name : $scope.name
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/integralStrategy/integralStrategyList/' + 1 + '/' + $scope.pageSize, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                if(data && data.data) {
                    $scope.integralStrategyList = data.data.list;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#loading').modal("close");
            }).error(function (err) {
                alert(err);
                $('#loading').modal("close");
            });
        };


    }]);